package com.wcgksoft.controller.greedLand;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.GreedLand.GreedLandAdministrator;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.EnterpriseProjectService;
import com.wcgksoft.service.GreedLand.GreedlandAdministratorService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 绿地管理 管理端
 *
 * @return
 */
@Controller
@RequestMapping(value = "greedLandAdministrator")
public class GreedLandAdministratorController {
    @Autowired
    private GreedlandAdministratorService greedlandAdministratorService;
    @Autowired
    private EnterpriseProjectService enterpriseProjectService;
    /**
     * 绿地管理列表-
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public Layui list(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "keyWord", required = false) String keyWord) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        DetachedCriteria criteria = DetachedCriteria.forClass(GreedLandAdministrator.class);
        criteria.add(Restrictions.eq("enterpriseid", sysUser.getEnterprise().getId()));
        if(StringUtils.isNotEmpty(keyWord)) {
            criteria.add(Restrictions.like("name", keyWord, MatchMode.ANYWHERE));
        }
        List<GreedLandAdministrator> list = greedlandAdministratorService.findByConditions(criteria, page, limit);
        if(list.size()>0){
            for (int i = 0;i<list.size();i++){
                //项目
                Integer departid = list.get(i).getDepartid();
                EnterpriseProject depart = enterpriseProjectService.findById(departid);
                list.get(i).setDepart(depart);
                //片区
                Integer areaid = list.get(i).getAreaid();
                EnterpriseProject area = enterpriseProjectService.findById(areaid);
                list.get(i).setArea(area);
            }
        }
        return Layui.data(0, "查询成功", greedlandAdministratorService.getTotalCount(), list);
    }

    /*
       查询项目
       */
    @ResponseBody
    @RequestMapping(value = "getDepart")
    public List<EnterpriseProject> getDepart() {
        DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseProject.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        criteria.createAlias("enterprise", "enterprise");
        criteria.add(Restrictions.eq("enterprise.id", sysUser.getEnterprise().getId()));
        criteria.add(Restrictions.eq("parentId", -1));
        List<EnterpriseProject> list = enterpriseProjectService.findByConditions(criteria);
        return list;
    }

    /*
   查询片区
   */
    @ResponseBody
    @RequestMapping(value = "getArea")
    public List<EnterpriseProject> getArea(@RequestParam(value = "departid") Integer departid) {
        DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseProject.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        criteria.createAlias("enterprise", "enterprise");
        criteria.add(Restrictions.eq("enterprise.id", sysUser.getEnterprise().getId()));
        criteria.add(Restrictions.eq("parentId", departid));
        List<EnterpriseProject> list = enterpriseProjectService.findByConditions(criteria);
        return list;
    }

    /*
     * 修改
     *
     * */
    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(HttpServletRequest request, @RequestParam(value = "data", required = false) String data) throws Exception {
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            GreedLandAdministrator greenLandAdministrator = (GreedLandAdministrator) JSONObject.toBean(JSONObject.fromObject(data), GreedLandAdministrator.class);
            String ids = String.valueOf(param.get("id"));
            if (ids.equals("")) {
                greenLandAdministrator.setState(0);
                greenLandAdministrator.setIsuse(0);
            }
            SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
            greenLandAdministrator.setEnterpriseid(sysUser.getEnterprise().getId());
            greedlandAdministratorService.saveOrUpdate(greenLandAdministrator);
            return Layui.data(0, "修改成功", 1, Layui.addList(greenLandAdministrator));
        }
    }

    /**
     * 停用
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="stop")
    public Layui stop(@RequestParam(value = "id") Integer id){
        GreedLandAdministrator byId = greedlandAdministratorService.findById(id);
        byId.setState(1);
        greedlandAdministratorService.upadte(byId);
        return Layui.data(1,"停用",1,null);
    }

    /**
     *  启用
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="start")
    public Layui start(@RequestParam(value = "id") Integer id){
        GreedLandAdministrator byId = greedlandAdministratorService.findById(id);
        byId.setState(0);
        greedlandAdministratorService.upadte(byId);
        return Layui.data(1,"启用",1,null);
    }

}
